package com.example.thecharacterbook;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyDataBase extends SQLiteOpenHelper {

    private Context context;
private static final String DATABASE_NAME ="MyDataBase2.db";
private static final int DATABASE_VERSION =1;
 static final String TABLE_NAME = "my_BookLibrary_InTant";
 static final String COLUMN_ID = "TheWordNumber";
 static final String COLUMN_WORD= "TheWord";
 static final String COLUMN_mean= "TheWord_Mean";
 static final String COLUMN_EXAMPLE= "Phrase";
 static final String COLUMN_EXAMPLE2= "Phrase2";
 static final String COLUMN_EXAMPLE3= "Phrase3";
    public MyDataBase(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context =context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE "+ TABLE_NAME +
                " (" + COLUMN_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_WORD + " TEXT, "+
                COLUMN_mean + " TEXT, " +
                COLUMN_EXAMPLE + " TEXT, " +
                COLUMN_EXAMPLE2 + " TEXT, " +
                COLUMN_EXAMPLE3 + " TEXT);";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    public void addWord(String word,String mean,String example,String example2,String example3){
    SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv =new ContentValues();
        cv.put(COLUMN_WORD,word);
        cv.put(COLUMN_mean,mean);
        cv.put(COLUMN_EXAMPLE,example);
        cv.put(COLUMN_EXAMPLE2,example2);
        cv.put(COLUMN_EXAMPLE3,example3);
        long result = db.insert(TABLE_NAME,null,cv);
        if(result==-1){
            Toast.makeText(context,"The add ERROR!",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(context,"Added Successful!",Toast.LENGTH_SHORT).show();
    }
    Cursor readAllData(){
        String query = "SELECT * FROM "+TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        if(db!=null){
            cursor = db.rawQuery(query,null);
        }
        return cursor;
    }
    void updateData(String id,String Word,String Mean,String EXAMPLE,String EXAMPLE1,String EXAMPLE2){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        Log.e("ID:","success1!");
        cv.put(COLUMN_ID,id);
        cv.put(COLUMN_WORD,Word);
        cv.put(COLUMN_mean,Mean);
        cv.put(COLUMN_EXAMPLE,EXAMPLE);
        cv.put(COLUMN_EXAMPLE2,EXAMPLE1);
        cv.put(COLUMN_EXAMPLE3,EXAMPLE2);
        Log.e("ID:","success2!");
        long result = db.update(TABLE_NAME,cv," TheWordNumber = ?",new String[] {id});
        if(result==-1){
            Toast.makeText(context,"UPDATE ERROR!",Toast.LENGTH_SHORT).show();

        }else
            Toast.makeText(context,"UPDATE Sucessful!",Toast.LENGTH_SHORT).show();

    }
    void updateData(String Word){
        SQLiteDatabase db = this.getWritableDatabase();

        long result = db.delete(TABLE_NAME," TheWord = ?",new String[] {Word});
        if(result==-1){
            Toast.makeText(context,"DELETE ERROR!",Toast.LENGTH_SHORT).show();

        }else
            Toast.makeText(context,"DELETE Sucessful!",Toast.LENGTH_SHORT).show();

    }
}
